#include <stdio.h>//qsot sort
#include <stdlib.h>

int int_cmp(const void*p1,const void*p2)
{
    return *(int*)p1 - *(int*)p2;
}

int char_cmp(const void*p1,const void*p2)
{
    return *(char*)p1 - *(char*)p2;
}

int main(void)
{
    int arr1[] = {1,3,6,5,4,2,9,8,7,10};
    char arr2[] = "acbgjdkszlorm";
    qsort(arr1,sizeof(arr1)/sizeof(arr1[0]),sizeof(int),int_cmp);
    qsort(arr2,sizeof(arr2)/sizeof(arr2[0]),sizeof(char),char_cmp);
    int i = 0;
    for(i=0;i<sizeof(arr1)/sizeof(arr1[0]);i++)
    {
        printf("%d ",arr1[i]);
    }
    printf("\n");
    for(i=0;i<sizeof(arr2)/sizeof(arr2[0]);i++)
    {
        printf("%c ",arr2[i]);
    }
    return 0;
}

//模拟实现qsort

int int_cmp(const void*p1,const void*p2)
{
    return *(int*)p1 - *(int*)p2;
}

void swap(void*p1,void*p2,int size)
{
    int i = 0;
    for(i=0;i<size;i++)//每队字节交换，实现每种数据的交换
    {
        char tmp = *((char*)p1+i);
        *((char*)p1+i) = *((char*)p2+i);
        *((char*)p2+i) = tmp;
    }
}

void bubble_sort(void*arr,int count,int size,int(*cmp)(const void*,const void*))
{
    int i = 0;
    int j = 0;
    for(i=0;i<count-1;i++)
    {
        for(j=0;j<count-i-1;j++)
        {
            if(cmp((char*)arr+j*size,(char*)arr+(j+1)*size)>0)
            {
                swap((char*)arr+j*size,(char*)arr+(j+1)*size,size);
            }
        }
    }
}

int main()
{
    int arr[] = {1,3,6,5,4,2,9,8,7,10};
    int i = 0;
    bubble_sort(arr,sizeof(arr)/sizeof(arr[0]),sizeof(int),int_cmp);
    for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}






















